package com.sch.www.oauth.controller;

import com.baidu.unbiz.fluentvalidator.ComplexResult;
import com.baidu.unbiz.fluentvalidator.FluentValidator;
import com.baidu.unbiz.fluentvalidator.ResultCollectors;
import com.sch.www.common.base.BaseController;
import com.sch.www.common.base.ResultBean;
import com.sch.www.common.validator.NotBlankFluentValidator;
import com.sch.www.oauth.orm.entity.manual.Client;
import com.sch.www.oauth.services.ClientServices;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RequestMapping("/v1/client")
@RestController
public class ClientController extends BaseController {

    @Autowired
    ClientServices clientServices;

    @GetMapping("/id/{id}")
    public ResultBean<Client> getById(@PathVariable String id) {
        ResultBean<Client> resultBean = ResultBean.newInstance();
        if (StringUtils.isBlank(id)) {
            resultBean.setError().setMsg("id不能为空。");
        } else {
            Client client = clientServices.selectById(id);
            resultBean.setSuccess().setData(client);
        }
        return resultBean;
    }


    @GetMapping("/clientId/{clientId}")
    public ResultBean<Client> getByName(@PathVariable String clientId) {
        ResultBean<Client> resultBean = ResultBean.newInstance();
        if (StringUtils.isBlank(clientId)) {
            resultBean.setError().setMsg("clientId不能为空。");
        } else {
            Client client = clientServices.selectByClientId(clientId);
            resultBean.setSuccess().setData(client);
        }
        return resultBean;
    }


    @PostMapping("/add")
    public ResultBean<Client> addClient(@RequestBody Client client) {
        ResultBean<Client> resultBean = ResultBean.newInstance();
        ComplexResult complexResult = FluentValidator.checkAll().failOver()
                .on(client.getClientId(), new NotBlankFluentValidator("ClientId"))
                .on(client.getClientSecret(), new NotBlankFluentValidator("ClientSecret"))
                .doValidate()
                .result(ResultCollectors.toComplex());

        if (!complexResult.isSuccess()) {
            return resultBean.setError().appendMsg(complexResult);
        }
        clientServices.insertSelective(client);
        return resultBean;
    }

    @PutMapping("/modify")
    public ResultBean<Client> modifyClient(@RequestBody Client client) {
        ResultBean<Client> resultBean = ResultBean.newInstance();
        ComplexResult complexResult = FluentValidator.checkAll().failOver()
                .on(client.getId(), new NotBlankFluentValidator("id"))
                .on(client.getClientId(), new NotBlankFluentValidator("ClientId"))
                .on(client.getClientSecret(), new NotBlankFluentValidator("ClientSecret"))
                .doValidate()
                .result(ResultCollectors.toComplex());
        if (!complexResult.isSuccess()) {
            return resultBean.setError().appendMsg(complexResult);
        }
        clientServices.updateByIdSelective(client);
        return resultBean;
    }

    @DeleteMapping("/remove/{id}")
    public ResultBean<Client> removeClient(@PathVariable String id) {
        ResultBean<Client> resultBean = ResultBean.newInstance();
        if (StringUtils.isBlank(id)) {
            resultBean.setError().setMsg("id不能为空。");
        } else {
            int result = clientServices.deleteById(id);
            if (result == 0) {
                resultBean.setError().setMsg("id不存在为空。");
            }
        }
        return resultBean;
    }
}
